//
//  ILCarViewController.m
//  ILife
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILCarViewController.h"
#import "ILCarListTableViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "AdView.h"
#import "ILCarListTableViewCell.h"
#import "ILCarDetailViewController.h"
#import "ILCarOtherViewController.h"
#import "FMDatabase.h"
#import "ILCarListNoPicTableViewCell.h"
#import "ILCarCollectionModel.h"

@interface ILCarViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIScrollView *titleScrollView; // 导航条上的标题栏
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIScrollView *carouselScrollView; // 轮播图
@property (nonatomic, strong) UITableView *listTableView; // 下面的列表
@property (nonatomic, strong) AdView *adView; // 轮播
@property (nonatomic, strong) NSMutableArray *listRowArr; // listTableView 的 row的个数 存放listModel
@property (nonatomic, strong) NSMutableArray *titleArr; // 标题的个数
@property (nonatomic, strong) ILCarOtherViewController *oneOtherVC;
@property (nonatomic, strong) ILCarOtherViewController *twoOtherVC;
@property (nonatomic, strong) ILCarOtherViewController *threeOtherVC;
@property (nonatomic, strong) ILCarOtherViewController *fourCarouselVC;
@property (nonatomic, strong) ILCarOtherViewController *fiveOtherVC;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) FMDatabase *dataBase; // 数据库
@property (nonatomic, strong) NSMutableArray *carouselArr;
@property (nonatomic, strong) NSMutableArray *carouselTitleArr;
@property (nonatomic, strong) NSMutableArray *carouselPicArr;
@property (nonatomic, copy) NSString *str;

@end

@implementation ILCarViewController

#pragma mark - 懒加载
- (UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 112)];
    }
    return _mainScrollView;
}



- (UIScrollView *)titleScrollView {
    if (!_titleScrollView) {
        self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH - 20, 44)];
    }
    return _titleScrollView;
}

- (UIView *)lineView {
    if (!_lineView) {
        self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, 40, 2)];
    }
    return _lineView;
}

- (AdView *)adView {
    if (!_adView) {
        self.adView = [[AdView alloc] init];
    }
    return _adView;
}

- (UIScrollView *)carouselScrollView {
    if (!_carouselScrollView) {
        self.carouselScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2)];
        _carouselScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 6, SCREEN_WIDTH / 2);
        _carouselScrollView.pagingEnabled = YES;
    }
    return _carouselScrollView;
}

- (UITableView *)listTableView {
    if (!_listTableView) {
        self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 112)];
    }
    return _listTableView;
}

- (NSMutableArray *)listRowArr {
    if (!_listRowArr) {
        self.listRowArr = [NSMutableArray array];
    }
    return _listRowArr;
}

- (NSMutableArray *)titleArr {
    if (!_titleArr) {
        self.titleArr = [NSMutableArray array];
    }
    return _titleArr;
}

- (ILCarOtherViewController *)oneOtherVC {
    if (!_oneOtherVC) {
        self.oneOtherVC = [[ILCarOtherViewController alloc] init];
    }
    return _oneOtherVC;
}

- (ILCarOtherViewController *)twoOtherVC {
    if (!_twoOtherVC) {
        self.twoOtherVC = [[ILCarOtherViewController alloc] init];
    }
    return _twoOtherVC;
}

- (ILCarOtherViewController *)threeOtherVC {
    if (!_threeOtherVC) {
        self.threeOtherVC = [[ILCarOtherViewController alloc] init];
    }
    return _threeOtherVC;
}

- (ILCarOtherViewController *)fourCarouselVC {
    if (!_fourCarouselVC) {
        self.fourCarouselVC = [[ILCarOtherViewController alloc] init];
    }
    return _fourCarouselVC;
}

- (ILCarOtherViewController *)fiveOtherVC {
    if (!_fiveOtherVC) {
        self.fiveOtherVC = [[ILCarOtherViewController alloc] init];
    }
    return _fiveOtherVC;
}
#pragma mark 标题请求数据
- (void)requestTitleData {
    // *************************************导航栏标题请求数据**********************************
    AFHTTPRequestOperationManager *titleManager = [AFHTTPRequestOperationManager manager];
    // AFNetWork2.0以上要添加下面的话
    titleManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
    NSDictionary *titleDataDic = @{@"deviceid":@"011a189ac2a",@"platform":@"iOS"};
    [titleManager POST:POST_TITLESCROLLVIEW_URL_STRING parameters:titleDataDic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [self.titleArr removeAllObjects];
        [_dataBase executeUpdate:@"DELETE FROM CARTITLE"];
        
        for (NSDictionary *titleDic in responseObject[@"data"]) {
            ILCarTitleModel *titleModel = [[ILCarTitleModel alloc] init];
            [titleModel setValuesForKeysWithDictionary:titleDic];
            [self.titleArr addObject:titleModel];
            [_dataBase executeUpdate:@"INSERT INTO CARTITLE(name, term_id, type) values(?,?,?)",titleModel.name,titleModel.term_id,titleModel.type];
        }
        [self setUpNaviButton];
        self.titleScrollView.contentSize = CGSizeMake(100 + 60 * _titleArr.count, 0);
        self.titleScrollView.showsHorizontalScrollIndicator = NO;
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"导航栏标题请求失败,失败原因:%@",error);
    }];
}
// 设置标题
- (void)setUpNaviButton {
    for (int i = 0; i < _titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(i * 60, 6, 55, 30);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        ILCarTitleModel *labelTextModel = _titleArr[i];
        button.tag = 200 + i;
        
        [button setTitle:labelTextModel.name forState:(UIControlStateNormal)];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        if (i == 0) {
            [button setTitleColor:[UIColor colorWithRed:0.108 green:0.501 blue:0.777 alpha:1.000] forState:(UIControlStateNormal)];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        } else {
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        }
        [button addTarget:self action:@selector(enterOther:) forControlEvents:(UIControlEventTouchUpInside)];
        [_titleScrollView addSubview:button];
    }
    
}

#pragma mark 轮播图请求数据
- (void)requestCarouselData {
    // ************************************轮播图请求数据*************************************
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // AFNetWork2.0以上要添加下面的话
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//设置相应内容类型
    NSDictionary *dataDic = @{@"classId":@"0",@"deviceid":@"011a189ac2a",@"platform":@"iOS"};
    [manager POST:POST_CAROUSEL_URL_STRING parameters:dataDic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [self.carouselPicArr removeAllObjects];
        [self.carouselTitleArr removeAllObjects];
        [self.carouselArr removeAllObjects];
        [_dataBase executeUpdate:@"DELETE FROM CARCAROUSEL"];
    for (NSDictionary *modelDic in responseObject[@"data"][@"focusList"]) {
            
            _carouselModel = [[ILCarouselModel alloc] init];
            [_carouselModel setValuesForKeysWithDictionary:modelDic];
            [_carouselPicArr addObject:_carouselModel.picUrl];
            [_carouselTitleArr addObject:_carouselModel.title];
            [_carouselArr addObject:self.carouselModel];
            [_dataBase executeUpdate:@"INSERT INTO CARCAROUSEL (picUrl, title, resourceLoc) VALUES (?,?,?)",_carouselModel.picUrl,_carouselModel.title,_carouselModel.resourceLoc];
        }
        
        [self setUpCarousel];
        
        
        
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"头条轮播图请求失败,失败原因:%@",error);
    }];
}
// 设置轮播图
- (void)setUpCarousel {
    //  轮播图
    self.adView = [AdView adScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2) imageLinkURL:_carouselPicArr placeHoderImageName:nil pageControlShowStyle:(UIPageControlShowStyleCenter)];
    // 轮播图上面的title
    [_adView setAdTitleArray:_carouselTitleArr withShowStyle:(AdTitleShowStyleLeft)];
    [self.carouselScrollView addSubview:_adView];
#pragma mark - 轮播图的方法
    __block ILCarViewController *test = self;
    self.adView.callBack = ^(NSInteger index,NSString * imageURL) {
        ILCarDetailViewController *detailVC = [[ILCarDetailViewController alloc] init];
        detailVC.collectionModel = [[ILCarCollectionModel alloc] init];
        ILCarouselModel *blockModel = test.carouselArr[index];
        detailVC.collectionModel.resourceLoc = blockModel.resourceLoc;
        detailVC.collectionModel.picUrl = blockModel.picUrl;
        detailVC.collectionModel.title = blockModel.title;
        [test.navigationController pushViewController:detailVC animated:YES];
    };

}

#pragma mark 列表请求数据
- (void)requestListTableViewData {
    // ***********************************下面的列表请求数据************************************
    AFHTTPRequestOperationManager *listManager =[AFHTTPRequestOperationManager manager];
    // AFNetWork2.0以上要添加下面的话
    listManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; //设置相应内容类型
    NSDictionary *listDataDic = @{@"classId":@"0",@"deviceid":@"011a189ac2a",@"page":[NSString stringWithFormat:@"%ld",(long)self.page],@"platform":@"iOS",@"userId":@"0"};
    [listManager POST:POST_LIST_URL_STRING parameters:listDataDic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if (self.page == 0) {
            [self.listRowArr removeAllObjects];
        }

        [_dataBase executeUpdate:@"DELETE FROM CARLIST"];
        for (NSDictionary *listDic in responseObject[@"data"][@"newsList"]) {
            _listModel = [[ILCarListModel alloc] init];
            [_listModel setValuesForKeysWithDictionary:listDic];
            if (_listModel.resourceLoc.length < 6) {
                [self.listRowArr addObject:_listModel];
            }
            [_dataBase executeUpdate:@"INSERT INTO CARLIST (title, publishTime, origin, readCount, picUrlList, resourceLoc) VALUES(?,?,?,?,?,?)",_listModel.title,_listModel.publishTime,_listModel.origin,_listModel.readCount,_listModel.picUrlList,_listModel.resourceLoc];
        }
        [self.listTableView reloadData];
        [self.listTableView.header endRefreshing];
        [self.listTableView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        NSLog(@"列表数据请求失败,失败原因:%@",error);
    }];

}



#pragma mark - 点击导航栏上面的标题的按钮进入详情
- (void)enterOther:(UIButton *)button {
    _mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH * (button.tag - 200), 0);
    [self changeView];
}
// 进入对应的tableView
- (void)changeView {

    NSInteger num = (NSInteger)_mainScrollView.contentOffset.x / SCREEN_WIDTH;
    switch (num) {
        case 0:
            _lineView.frame = CGRectMake(0, 41, 40, 2);
            _titleScrollView.contentOffset = CGPointMake(0, 0);
            [self isHereButton:[_titleScrollView viewWithTag:200] tag:0];
            break;
        case 1:
            [self changeViewWithTag:1 labelTitleModel:_titleArr[1] otherVC:self.oneOtherVC button:[_titleScrollView viewWithTag:1 + 200]];
            break;
        case 2:
            [self changeViewWithTag:2 labelTitleModel:_titleArr[2] otherVC:_twoOtherVC button:[_titleScrollView viewWithTag:2 + 200]];
            _titleScrollView.contentOffset = CGPointMake(60, 0);
            break;
        case 3:
            [self changeViewWithTag:3 labelTitleModel:_titleArr[3] otherVC:_threeOtherVC button:[_titleScrollView viewWithTag:3 + 200]];
            break;
        case 4:
            [self changeViewWithTag:4 labelTitleModel:_titleArr[4] otherVC:_fourCarouselVC button:[_titleScrollView viewWithTag:4 + 200]];
            break;
        case 5:
            [self changeViewWithTag:5 labelTitleModel:_titleArr[5] otherVC:_fiveOtherVC button:[_titleScrollView viewWithTag:5 + 200]];
            _titleScrollView.contentOffset = CGPointMake(100 * 375 / SCREEN_WIDTH, 0);
            break;
        case 6:
            _lineView.frame = CGRectMake(0, 41, 40, 2);
            _titleScrollView.contentOffset = CGPointMake(0, 0);
            [self isHereButton:[_titleScrollView viewWithTag:200] tag:0];
            self.mainScrollView.contentOffset = CGPointMake(0, 0);
            break;
        default:
            break;
    }
}

- (void)changeViewWithTag:(NSInteger)tag labelTitleModel:(ILCarTitleModel *)labelTitleModel otherVC:(ILCarOtherViewController *)otherVC button:(UIButton *)button {
    
    
    labelTitleModel = _titleArr[tag];
    otherVC.carVC = self; // self推出详情界面
    otherVC.classId = labelTitleModel.term_id;
    otherVC.num = tag;
    [otherVC saveOtherDataWithNum:tag];
    [otherVC requestDataWithNum:tag];
    _lineView.frame = CGRectMake(60 * tag, 42, 40, 2);
    if (tag == 5) {
        _lineView.frame = CGRectMake(60 * 5, 42, 55, 2);
    }
    [self isHereButton:button tag:tag];
}

/**
 *   遍历一下标题上面的button 如果跟当前的button是同一个 则改变颜色和大小 否则还是原来的样式
 */
- (void)isHereButton:(UIButton *)button tag:(NSInteger)tag {
    for (int i = 0; i < _titleArr.count; i++) {
        button = [_titleScrollView viewWithTag:i + 200];
        if (i == tag) {
            [button setTitleColor:[UIColor colorWithRed:0.108 green:0.501 blue:0.777 alpha:1.000] forState:(UIControlStateNormal)];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        } else {
            [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        }
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    _carouselArr = [NSMutableArray array];
    _carouselPicArr = [NSMutableArray array];
    _carouselTitleArr = [NSMutableArray array];
#pragma mark - 把这个给关了 否则tableView会整体往下移
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor= [UIColor whiteColor];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    self.navigationItem.titleView = view;
    self.lineView.backgroundColor = [UIColor colorWithRed:0.108 green:0.501 blue:0.777 alpha:1.000];
    [self.titleScrollView addSubview:self.lineView];
    [view addSubview:self.titleScrollView];
    
    
    self.mainScrollView.showsHorizontalScrollIndicator = NO;
    self.mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 7, 0);
    self.mainScrollView.pagingEnabled = YES;
    self.mainScrollView.delegate = self;
  
    self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 112)style:(UITableViewStylePlain)];
    [self.mainScrollView addSubview:_listTableView];
    
    [self.view addSubview:self.mainScrollView];

    
    self.listTableView.tableHeaderView = self.carouselScrollView;
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    [self.listTableView registerClass:[ILCarListTableViewCell class] forCellReuseIdentifier:@"carListCell"];
    [self.listTableView registerClass:[ILCarListNoPicTableViewCell class] forCellReuseIdentifier:@"noPic"];
    
    [self setUpOtherView:self.oneOtherVC num:1];
    [self setUpOtherView:self.twoOtherVC num:2];
    [self setUpOtherView:self.threeOtherVC num:3];
    [self setUpOtherView:self.fourCarouselVC num:4];
    [self setUpOtherView:self.fiveOtherVC num:5];
    
    // 回到顶部按钮
    UIButton *topButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [topButton setImage:[UIImage imageNamed:@"top"] forState:(UIControlStateNormal)];
    topButton.frame = CGRectMake(SCREEN_WIDTH - 60, SCREEN_HEIGHT - 100, 40, 40);
    topButton.backgroundColor = [UIColor grayColor];
    topButton.alpha = 0.5;
    topButton.layer.cornerRadius = 20;
    topButton.layer.masksToBounds = YES;
    [topButton addTarget:self action:@selector(toTop) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:topButton];

    
    [self saveData];

    [self requestCarouselData];
    [self requestListTableViewData];
    [self requestTitleData];
    [self refresh];
}

// 返回顶部
- (void)toTop {
//    if (self.mainScrollView.contentOffset.x == 0) {
//        [self.listTableView scrollRectToVisible:CGRectMake(0, 20, 1, 1) animated:NO];
//    } else {
//        ILCarOtherViewController *otherVC = [[ILCarOtherViewController alloc] init];
//        [otherVC.listTableView scrollRectToVisible:CGRectMake(0, 20, 1, 1) animated:YES];
//    }
    NSInteger num = self.mainScrollView.contentOffset.x / SCREEN_WIDTH;
    CGFloat height = 20;
    switch (num) {
        case 0:
            [self.listTableView scrollRectToVisible:CGRectMake(0, height, 1, 1) animated:YES];
            break;
        case 1:
            [_oneOtherVC.listTableView scrollRectToVisible:CGRectMake(0, height, 1,1) animated:YES];
            break;
        case 2:
            [_twoOtherVC.listTableView scrollRectToVisible:CGRectMake(0, height, 1, 1) animated:YES];
            break;
        case 3:
            [_threeOtherVC.listTableView scrollRectToVisible:CGRectMake(0, height, 1, 1) animated:YES];
            break;
        case 4:
            [_fourCarouselVC.listTableView scrollRectToVisible:CGRectMake(0, height, 1, 1) animated:YES];
            break;
        case 5:
            [_fiveOtherVC.listTableView scrollRectToVisible:CGRectMake(0, height, 1, 1) animated:YES];
            break;
        default:
            break;
    }
    
}


/**
 *  保存数据
 */
- (void)saveData {
    _dataBase = [FMDatabase databaseWithPath:FILEPATH];
    if ([_dataBase open]) {
        NSLog(@"数据库打开成功");
        [_dataBase executeUpdate:@"CREATE TABLE CARCAROUSEL (picUrl text, title text, resourceLoc text)"];
        [_dataBase executeUpdate:@"CREATE TABLE CARTITLE (name text, term_id text, type text)"];
        [_dataBase executeUpdate:@"CREATE TABLE CARLIST (title text, publishTime text, origin text, readCount text, picUrlList text, resourceLoc text)"];
    } else {
        NSLog(@"数据库打开失败");
    }
    
    FMResultSet *carouselSet =[_dataBase executeQueryWithFormat:@"SELECT *FROM CARCAROUSEL"];
    while ([carouselSet next]) {
        NSString *picUrl = [carouselSet stringForColumn:@"picUrl"];
        NSString *title = [carouselSet stringForColumn:@"title"];
        NSString *resourceLoc = [carouselSet stringForColumn:@"resourceLoc"];
        ILCarouselModel *carouselModel = [[ILCarouselModel alloc] init];
        carouselModel.picUrl = picUrl;
        carouselModel.title = title;
        carouselModel.resourceLoc = resourceLoc;
       
        
        [self.carouselPicArr addObject:carouselModel.picUrl];
        [self.carouselTitleArr addObject:carouselModel.title];
        [self.carouselArr addObject:carouselModel];
    }
    [carouselSet close];
    
    FMResultSet *titleSet = [_dataBase executeQueryWithFormat:@"SELECT *FROM CARTITLE"];
    while ([titleSet next]) {
        NSString *name = [titleSet stringForColumn:@"name"];
        NSString *term_id = [titleSet stringForColumn:@"term_id"];
        NSString *type = [titleSet stringForColumn:@"type"];
        ILCarTitleModel *titleModel = [[ILCarTitleModel alloc] init];
        titleModel.name = name;
        titleModel.term_id = term_id;
        titleModel.type = type;
        [self.titleArr addObject:titleModel];
       
    }
    // 判断网络是否连接
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusNotReachable && self.mainScrollView.contentOffset.x == 0) {
            [self setUpCarousel];
            [self setUpNaviButton];
        }
    }];
    [manager.reachabilityManager startMonitoring];
    [titleSet close];
    
    FMResultSet *listset = [_dataBase executeQueryWithFormat:@"SELECT *FROM CARLIST"];
    while ([listset next]) {
        NSString *title = [listset stringForColumn:@"title"];
        NSString *publishTime = [listset stringForColumn:@"publishTime"];
        NSString *origin = [listset stringForColumn:@"origin"];
        NSString *readCount = [listset stringForColumn:@"readCount"];
        NSString *picUrlList = [listset stringForColumn:@"picUrlList"];
        NSString *resourceLoc = [listset stringForColumn:@"resourceLoc"];
        ILCarListModel *listModel = [[ILCarListModel alloc] init];
        listModel.title = title;
        listModel.publishTime = publishTime;
        listModel.origin = origin;
        listModel.readCount = readCount;
        listModel.picUrlList = picUrlList;
        listModel.resourceLoc = resourceLoc;
        [self.listRowArr addObject:listModel];
    }
    [listset close];
}


/**
 *  下拉刷新 上提加载
 */
- (void)refresh {
    // 下拉刷新
    self.listTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.listTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
//    [self.listTableView.header beginRefreshing];
    // 上提加载
    self.listTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
    }];
    self.listTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

- (void)headerRefresh {
    self.page = 0;
    [self requestListTableViewData];
}

- (void)footerRefresh {
    self.page += 1;
    [self requestListTableViewData];
}

/**
 *  设置其他页面
 */
- (void)setUpOtherView:(ILCarOtherViewController *)otherVC num:(NSInteger)num {

    otherVC.view.frame = CGRectMake(SCREEN_WIDTH * num, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 112);
    [self.mainScrollView addSubview:otherVC.view];
}

#pragma mark - listTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listRowArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ILCarListModel *listModel = _listRowArr[indexPath.row];
    if ([listModel.picUrlList isEqualToString:@""]) {
        ILCarListNoPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"noPic" forIndexPath:indexPath];
        cell.listModel = _listRowArr[indexPath.row];
        return cell;
    }
    ILCarListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carListCell" forIndexPath:indexPath];
   
    cell.listModel = _listRowArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH / 4 + 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ILCarListModel *listModel = self.listRowArr[indexPath.row];
    ILCarDetailViewController * carDetailVC = [[ILCarDetailViewController alloc] init];
    carDetailVC.collectionModel = [[ILCarCollectionModel alloc] init];
    carDetailVC.collectionModel.resourceLoc = listModel.resourceLoc;
    carDetailVC.collectionModel.picUrl = listModel.picUrlList;
    carDetailVC.collectionModel.title = listModel.title;
    [self.navigationController pushViewController:carDetailVC animated:YES];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self changeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
